package com.hsc.db.tools;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hsc.account.oneActivity;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class ItemService {
	public  DBOpenHelper dbOpenHelper;
	public  SQLiteDatabase db=null;
	public ItemService(Context context)
	{
		dbOpenHelper = new DBOpenHelper(context);
//	    db = dbOpenHelper.getWritableDatabase();
		db = oneActivity.db;
	}

	public void save(ItemList item){
//		SQLiteDatabase  db = dbOpenHelper.getWritableDatabase();

		db.execSQL("insert into item(type,flag) values(?,?)",
				new Object[]{item.getType(),item.getFlag()});
	}



	public void delete (Integer id){
//		 SQLiteDatabase  db = dbOpenHelper.getWritableDatabase();

		db.execSQL("delete from item where id=?",
				new Object[]{id});
	}

	public void deleteAll (){
//		 SQLiteDatabase  db = dbOpenHelper.getWritableDatabase();

		db.execSQL("delete from item ");
	}



	public List<Map<String, Object>> getlData(String flag)
	{

		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
//		SQLiteDatabase  db = dbOpenHelper.getReadableDatabase();
		Cursor cur = db.rawQuery("select * from item where flag=? ",
				new String[]{flag});
		while(cur.moveToNext())
		{
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("text", cur.getString(cur.getColumnIndex("type")));
			map.put("_id", cur.getInt(cur.getColumnIndex("id")));
			list.add(map);

			Log.i("itemtest", cur.getString(cur.getColumnIndex("type")));

		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("text", "新增");
		list.add(map);
		cur.close();
		return list;
	}

	public ItemList getlDataFlag(String flag)
	{

		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
//		SQLiteDatabase  db = dbOpenHelper.getReadableDatabase();
		Cursor cur = db.rawQuery("select * from item where flag=? ",
				new String[]{flag});
		while(cur.moveToNext())
		{
			ItemList item = new ItemList();
			item.setFlag(cur.getString(cur.getColumnIndex("flag")));
			item.setType(cur.getString(cur.getColumnIndex("type")));
			cur.close();
			return item;
		}


		cur.close();
		return null;
	}

}
